package com.jwdf.uda.ui.fragment.garb;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.LayFindBinding;
import com.jwdf.uda.event.FollowEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.HomeActorListAction;
import com.jwdf.uda.ui.activity.FindCategoryActivity;
import com.jwdf.uda.ui.activity.SearchActivity;
import com.jwdf.uda.ui.adapter.ActorAdapter;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.AppBus;
import com.jwdf.uda.utils.DisplayUtils;
import com.jwdf.uda.view.XListView;
import com.squareup.otto.Subscribe;

import java.util.List;

import retrofit2.Response;

import static com.jwdf.uda.R.id.fl_input;
import static com.jwdf.uda.R.id.vActorRefresh;

/**
 * Created by RD-001 on 2016/4/27.
 */
public class FindFragment extends BaseFragment implements View.OnClickListener {
    private LayFindBinding mDataBinding;
    private ActorAdapter adapter;

    public static FindFragment instance() {
        FindFragment view = new FindFragment();
        return view;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mDataBinding = LayFindBinding.inflate(inflater, container, false);
        LinearLayout.LayoutParams flParams = (LinearLayout.LayoutParams) mDataBinding.lySearch.getRoot().getLayoutParams();
        flParams.height += DisplayUtils.getStatusBarHeight(getContext());
        mDataBinding.lySearch.getRoot().setLayoutParams(flParams);
        mDataBinding.lySearch.getRoot().setPadding(0, DisplayUtils.getStatusBarHeight(getContext()), 0, 0);
        mDataBinding.lySearch.flInput.setOnClickListener(this);
        initViews();
        return mDataBinding.getRoot();
    }

    private void initViews() {
        adapter = new ActorAdapter(getActivity(), this);
        mDataBinding.lv.setPullRefreshEnable(false);
//        mDataBinding.lv.setXListViewListener(createOnPullListener());
        final View vFooter = getActivity().getLayoutInflater().inflate(R.layout.view_find_footer, null);
        mDataBinding.lv.addFooterView(vFooter);
        mDataBinding.lv.setAdapter(adapter);
        requestData();

        initVHeader();

        AppBus.getInstance().register(this);
    }

    private void initVHeader() {
        final View vHeader = getActivity().getLayoutInflater().inflate(R.layout.header_find, null);
        mDataBinding.lv.addHeaderView(vHeader);

        final View vDress = vHeader.findViewById(R.id.vDress);
        final View vMakeUp = vHeader.findViewById(R.id.vMakeUp);
        final View vLife = vHeader.findViewById(R.id.vLife);
//        final View vShopping = vHeader.findViewById(R.id.vShopping);
        final View vEmotion = vHeader.findViewById(R.id.vEmotion);

        vDress.setTag(FindCategoryActivity.DRESS);
        vMakeUp.setTag(FindCategoryActivity.MAKEUP);
        vLife.setTag(FindCategoryActivity.LIFE);
//        vShopping.setTag(FindCategoryActivity.SHOPPING);
        vEmotion.setTag(FindCategoryActivity.EMOTION);

        vDress.setOnClickListener(this);
        vMakeUp.setOnClickListener(this);
        vLife.setOnClickListener(this);
//        vShopping.setOnClickListener(this);
        vEmotion.setOnClickListener(this);
        vHeader.findViewById(R.id.vActorRefresh).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        if (id == R.id.vDress || id == R.id.vMakeUp || id == R.id.vLife || id == R.id.vEmotion) {
            final int tag = (int) v.getTag();
            FindCategoryActivity.start(getContext(), tag);
        } else if (id == fl_input) {
            startActivityFromFragment(SearchActivity.class);
        } else if (id == vActorRefresh) {
            requestData();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        AppBus.getInstance().unregister(this);
    }

    private XListView.IXListViewListener createOnPullListener() {
        return new XListView.IXListViewListener() {
            @Override
            public void onRefresh(XListView xListView) {
                requestData();
            }

            @Override
            public void onLoadMore(XListView xListView) {
                requestData();
            }
        };
    }

    public void onHeadActionClick(View view) {
    }

    @Subscribe
    public void onChanged(FollowEvent event) {
        List<PbBean.PbUserSimple> list = adapter.getData();

        boolean isRefresh = false;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUserId().equals(event.getUserId())){
                isRefresh = true;
                break;
            }
        }
        if (isRefresh) requestData();
    }

    private void requestData() {
        onNewRequestCall(new HomeActorListAction(getContext()).request(new AHttpService.IResCallback<PbBean.PbResPageGetHomeActorList>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResPageGetHomeActorList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_operation))) {
                        return;
                    }
                    doSuccessResponse(response.body());
                }
            }

        }));
    }

    private void doSuccessResponse(PbBean.PbResPageGetHomeActorList body) {
        if (body != null) {
            adapter.setData(body.getActorListList());
            mDataBinding.lv.stopRefresh();
            mDataBinding.lv.stopLoadMore();
        }
    }

}